//
//  FilterAttr_Vc.swift
//  Pods
//
//  Created by eme on 2017/3/30.
//
//


/*
 owner:cy
 update:2017年03月30日16:30:47
 info: 类目筛选界面
 */
import RxSwift
import UtilCore
import EmptyDataView
import RickonCore

class FilterAttr_Vc: Empty_TVc {
    
    /****************************Storyboard UI设置区域****************************/
    @IBOutlet weak var filter_Tv: UITableView!
    
    @IBOutlet weak var reset_Btn: UIButton!
    
    @IBOutlet weak var submit_Btn: UIButton!
    /*----------------------------   自定义属性区域    ----------------------------*/
    
    var palist: [PalistModel]?
    /// 点击重置和确定按钮时都会触发
    var didSelectPaModel_Closure:((_ palist: [PalistModel]?) ->Void)?
    ///用于缓存展开的哪一行
    var spreadedSection:Int = -1
    /****************************Storyboard 绑定方法区域****************************/
    
    
    
    /**************************** 以下是方法区域 ****************************/
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
    }
    /**
     界面基础设置
     */
    override func setupUI() {
        /**
         *  自定义 导航栏左侧 返回按钮
         */
        self.customLeftBarButtonItem()
        self.showEmptyView = true
        self.filter_Tv.dzn_tv_emptyDelegate = self
        self.filter_Tv.dzn_tv_emptyDataSource = self
        self.submit_Btn
            .rx.tap
            .subscribe(onNext: { [unowned self] ( _ ) in
                if let didSelectPaModel_Closure = self.didSelectPaModel_Closure{
                    didSelectPaModel_Closure(self.palist)
                }
                _ = self.navigationController?.popViewController(animated: true)
            })
            .disposed(by: disposeBag)
        self.reset_Btn
            .rx.tap
            .subscribe(onNext: { [unowned self] ( _ ) in
                self.resetPalist()
            })
            .disposed(by: disposeBag)
    }
    /**
     app 主题 设置
     */
    override func setViewTheme(){
        
    }
    /**
     绑定到viewmodel 设置
     */
    override func bindToViewModel(){
        
    }
}
extension FilterAttr_Vc{
    /// 重置数据
    func resetPalist( ) -> Void {
        if  let palist = self.palist {
            for item in palist{
                for subItem in item.pavalues{
                    subItem.hasSelected = false
                }
            }
        }
        self.filter_Tv.reloadData()
    }
}
// MARK: UITableViewDelegate
extension FilterAttr_Vc :UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        if section ==  self.spreadedSection {
            return palist![section].pavalues.count
        }else{
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "FilterAttr_TCell", for: indexPath) as! FilterAttr_TCell
        cell.item = palist![indexPath.section].pavalues[indexPath.row]
        return cell
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        guard let palist = self.palist  else {
            return 0
        }
        return palist.count
    }
    
}
// MARK: UITableViewDelegate
extension FilterAttr_Vc: UITableViewDelegate {
    /// 点击行事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        let subItem = palist![indexPath.section].pavalues[indexPath.row]
        subItem.hasSelected = !subItem.hasSelected
        self.filter_Tv.reloadData()
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        return 41
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{
        return 45
    }
    ///自定义view
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let cell = self.filter_Tv.dequeueReusableCell(withIdentifier: "FilterAttr_HeaderV") as! FilterAttr_HeaderV
        cell.item = palist![section]
        cell.updateSpeadState(section == self.spreadedSection)
        cell.didSpreadBtn_Closure = {[unowned self] _ in
            if self.spreadedSection == section {
                self.spreadedSection = -1
            }else{
                self.spreadedSection = section
            }
            self.filter_Tv.reloadData()
        }
        return cell
    }
    
}
/// 空数据数据代理
extension FilterAttr_Vc :EmptyDataSource{
    func description(emptyView scrollView: UIScrollView) -> NSAttributedString?{
        return Theme.getEmptyDesc("暂无结果")
    }
    func image(emptyView scrollView: UIScrollView) -> UIImage?{
        return UIImage(named: "property-empty", in: RobbCore.bundle, compatibleWith: nil)
    }
    func backgroundColor(emptyView scrollView: UIScrollView) -> UIColor?{
        return Theme.v.bg_color
    }
}

